package com.hb.util;

import org.springframework.util.Base64Utils;

import java.nio.charset.StandardCharsets;

/**
 * @description:
 * @projectName: zhb-frame-build
 * @author: zhouhaizhi
 * @createTime: 2021/8/27 16:30
 * @version:1.0
 */
public class Base64Util {

    /**
     * @描述 Base64 解密
     * @创建人 zhouhaizhi
     * @创建时间 2021/8/27 16:32
     * @参数
     * @返回值
     * @修改人和其它信息
     */
    public static byte[] decryptBase64(byte[] key) throws Exception {
        return Base64Utils.decode(key);
    }

    /**
     * @描述 Base64 加密
     * @创建人 zhouhaizhi
     * @创建时间 2021/8/27 16:33
     * @参数
     * @返回值
     * @修改人和其它信息
     */
    public static String encryptBase64(byte[] key) throws Exception {
        return new String(Base64Utils.encode(key));
    }

    public static void main(String[] args) {
        String jsonStr = "admin:123456";
        String jiami = "eyJhdWQiOlsiemhiLWZyYW1lLWJ1aWxkIl0sInNjb3BlIjpbIndyaXRlIiwicmVhZCJdLCJleHAiOjE2MzMxNTU0NjIsImp0aSI6IjYxNjhhOWU2LTZiMmUtNDdkOS1iYWY3LTZmYWQ4ZDlhNDhiMSIsImNsaWVudF9pZCI6ImFkbWluIn0";
//        try {
//            jiami = Base64Util.encryptBase64(jsonStr.getBytes());
//            System.out.println("Base64加密后：" + jiami);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        String jiemi = "";
        try {
            byte[] jm = Base64Util.decryptBase64(jiami.getBytes(StandardCharsets.UTF_8));
            jiemi = new String(jm, StandardCharsets.UTF_8);
            System.out.println("Base64解密后：" + jiemi);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
